/*
 * MWizzar.java
 *
 * Created on 6 de diciembre de 2007, 11:34 AM
 */
package ig.metrica.ui.matriz.correlacion.calculada;

import ig.metrica.ui.matriz.correlacion.calculada.modelo.IModeloWizzar;
import ig.util.NoContinuaException;
import java.util.Hashtable;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author  Jimmy
 */
public class MWizzar extends javax.swing.JDialog {

    Hashtable<Integer, JPanel> paneles = new Hashtable<Integer, JPanel>();
    /** Creates new form MWizzar */
    private Integer actual = 1;

    public MWizzar(java.awt.Frame parent) throws Exception {
        super(parent, true);
        initComponents();
        paneles.put(1, new PInicial(this));
        paneles.put(2, new PFactores(this));
        paneles.put(3, new PDatosBasicos(this));
        Siguiente.setEnabled(true);
        Cancelar.setEnabled(true);
        this.PCentral.add(paneles.get(1));
        this.LOperacion.setText(paneles.get(actual).getName());

        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);

        setResizable(true);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        LOperacion = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        Finalizar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();
        Siguiente = new javax.swing.JButton();
        Anterior = new javax.swing.JButton();
        PCentral = new javax.swing.JPanel();

        setTitle("Asistente para calcular Matrices de Correlaci\\u00F3n");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Matriz de Correlación Calculada");

        LOperacion.setFont(new java.awt.Font("Dialog", 3, 12));
        LOperacion.setForeground(new java.awt.Color(0, 0, 153));
        LOperacion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LOperacion.setText("Operaci\\u00F3n");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(LOperacion, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(LOperacion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        Finalizar.setFont(new java.awt.Font("Dialog", 0, 11));
        Finalizar.setText("Finalizar");
        Finalizar.setEnabled(false);
        Finalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FinalizarActionPerformed(evt);
            }
        });

        Cancelar.setFont(new java.awt.Font("Dialog", 0, 11));
        Cancelar.setText("Cancelar");
        Cancelar.setEnabled(false);
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        Siguiente.setFont(new java.awt.Font("Dialog", 0, 11));
        Siguiente.setText("Siguiente >");
        Siguiente.setEnabled(false);
        Siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SiguienteActionPerformed(evt);
            }
        });

        Anterior.setFont(new java.awt.Font("Dialog", 0, 11));
        Anterior.setText("< Anterior");
        Anterior.setEnabled(false);
        Anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnteriorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(162, Short.MAX_VALUE)
                .addComponent(Anterior, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Siguiente, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Finalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Finalizar)
                    .addComponent(Cancelar)
                    .addComponent(Siguiente)
                    .addComponent(Anterior))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        PCentral.setBackground(new java.awt.Color(255, 0, 0));
        PCentral.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(PCentral, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PCentral, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void FinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FinalizarActionPerformed
        try {
            // TODO add your handling code here:
            Siguiente.setEnabled(false);
            Anterior.setEnabled(false);
            Finalizar.setEnabled(false);
            Cancelar.setEnabled(false);
            ((IModeloWizzar) paneles.get(actual)).finalizar();
            while (!((IModeloWizzar) paneles.get(actual)).isSiguiente()) {
                Thread.sleep(1000);
            }
            if (((IModeloWizzar) paneles.get(actual)).getErrores() != null) {
                JOptionPane.showMessageDialog(this, ((IModeloWizzar) paneles.get(actual)).getErrores());
                Anterior.setEnabled(true);
                Finalizar.setEnabled(true);
                Cancelar.setEnabled(true);                
            } else {
                this.dispose();
            }
        } catch (Exception ex) {//GEN-LAST:event_FinalizarActionPerformed
            ex.printStackTrace();
        }
}                                         

    public Object getObject() {
        return ((IModeloWizzar) paneles.get(actual)).getObject();
    }
    /**
     * El performer del boton siguiente
     * @param evt
     */
    private void SiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SiguienteActionPerformed
        // TODO add your handling code here:
        if (actual < paneles.size()) {
            try {
                ((IModeloWizzar) paneles.get(actual)).siguiente();

                while (!((IModeloWizzar) paneles.get(actual)).isSiguiente()) {
                    Thread.sleep(1000);//espera hasta que el panel le conteste que puede seguir
                }

                if (((IModeloWizzar) paneles.get(actual)).getErrores() != null) {
                    JOptionPane.showMessageDialog(this, ((IModeloWizzar) paneles.get(actual)).getErrores());
                } else {
                    Object obj = ((IModeloWizzar) paneles.get(actual)).getObject();
                    this.PCentral.remove(paneles.get(actual));
                    this.PCentral.add(paneles.get(++actual));
                    ((IModeloWizzar) paneles.get(actual)).setObject(obj);
                    this.LOperacion.setText(paneles.get(actual).getName());
                    this.PCentral.updateUI();
                    if (actual.intValue() == paneles.size()) {
                        Siguiente.setEnabled(false);
                        Finalizar.setEnabled(true);
                    }
                    Anterior.setEnabled(true);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
}//GEN-LAST:event_SiguienteActionPerformed

    private void AnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnteriorActionPerformed
        // TODO add your handling code here:
        if (actual > 1) {
            this.PCentral.remove(paneles.get(actual));
            this.PCentral.add(paneles.get(--actual));
            this.LOperacion.setText(paneles.get(actual).getName());
            this.PCentral.updateUI();
            if (actual.intValue() == 1) {
                Anterior.setEnabled(false);
            }
            Finalizar.setEnabled(false);
            Siguiente.setEnabled(true);
        }
    }//GEN-LAST:event_AnteriorActionPerformed
    private boolean cancelado=false;

    public boolean isCancelado() {
        return cancelado;
    }
    
    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        // TODO add your handling code here:
        cancelado=true;
        this.dispose();
    }//GEN-LAST:event_CancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Anterior;
    private javax.swing.JButton Cancelar;
    private javax.swing.JButton Finalizar;
    private javax.swing.JLabel LOperacion;
    private javax.swing.JPanel PCentral;
    private javax.swing.JButton Siguiente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

}
